package tech.dingxin.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.opencv.opencv_core.Mat;
import tech.dingxin.protobuf.FrameProtobuf;

import static org.bytedeco.opencv.global.opencv_imgcodecs.imencode;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;

/**
 * @author dingxin
 */
public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(System.currentTimeMillis());
        String filename = "A:\\nrvs\\src\\main\\resources\\2.JPG";
        Mat image = imread(filename);
        BytePointer bytePointer = new BytePointer();
        imencode(".jpg", image, bytePointer);
        String strEncode = bytePointer.getString();
        FrameProtobuf.Frame frame = FrameProtobuf.Frame.newBuilder()
                .setData(strEncode)
                .build();
        System.out.println(System.currentTimeMillis());
        ctx.writeAndFlush(frame);
    }
}
